//
//  Array-cut.swift
//  Pods
//
//  Created by zhu xietong on 2017/8/12.
//
//

import Foundation




extension Array{
    fileprivate class SamplingStep{
        var element:Element
        var lower:Double
        var upper:Double
        var index:Int = -1
        var valid:Bool = false
        func checkValid(steps:[Double]) -> Bool {
            for step in steps {
                if (step >= lower && step <= upper){
                    self.valid = true
                    return true
                }
            }
            self.valid = false
            return false
        }
        init(element:Element,lower:Double,upper:Double) {
            self.element = element
            self.lower = lower
            self.upper = upper
        }
    }
    
    //MARK: 取样函数
    public func sampling(limit:Int)->Array {
        if self.count <= limit{
            return self
        }
        if limit < 1{
            return []
        }
                
        let step:Double = Double(count)/Double(limit)
        
        let r:Range<Int> = 0..<limit
        let samplingPoints = r.map { i -> Double in
            return Double(i) * step
        }
        
        var items:[SamplingStep] = []
        for (i,item) in self.enumerated() {
            let stepLeft = Double(i) -  0.4999999999999
            let stepRight = Double(i) + 0.5000000000000
            
            let samp = SamplingStep(element: item, lower: stepLeft, upper: stepRight)
            samp.index = i
            _ = samp.checkValid(steps: samplingPoints)
            items.append(samp)
            
        }
        
        items = items.filter { item in
            return item.valid
        }
        var items_:[SamplingStep] = []
        for item in items {
            if  !items_.contains(where: { one in
                one.index == item.index
            }){
                items_.append(item)
            }
        }
        return items_.map { step in
            return step.element
        }
    }
}


extension Array{
    public subscript(_ from:Int?,_ to:Int?) ->Array{
        var newList = [Element]()
        
        
        let f = from ?? 0
        let t = to ?? self.count
        for (i,obj) in self.enumerated()
        {
            if i >= f && i <= t
            {
                newList.append(obj)
            }
        }
        return newList
    }
}
extension Array {
    
    // 去重
    public func filterDuplicate()->[Element]
    {
        var result = [Element]()
        
        var keys = [String]()
        for one in self
        {
            if keys.contains("\(one)")
            {
                continue
            }
            else{
                keys.append("\(one)")
                result.append(one)
            }
        }
        return result
    }
    public func filterDuplicates<E: Equatable>(_ filter: (Element) -> E) -> [Element] {
        var result = [Element]()
        for value in self {
            let key = filter(value)
            if !result.map({filter($0)}).contains(key) {
                result.append(value)
            }
        }
        return result
        //        let arrays = ["1", "2", "2", "3", "4", "4"]
        //        let filterArrays = arrays.filterDuplicates({$0})
        //        print(filterArrays)
        //
        //        let modelArrays = [1,2,3,4,4,2]
        //        let filterModels = modelArrays.filterDuplicates({$0})
        //        print(filterModels)
    }
    
}





extension NSArray{
    public subscript(_ from:Int?,_ to:Int?) ->NSMutableArray{
        let f = from ?? 0
        let t = to ?? self.count

        let newList = NSMutableArray()
        for (i,obj) in self.enumerated()
        {
            if i >= f && i <= t
            {
                newList.add(obj)
            }
        }
        return newList
    }
}
